package com.example.wiki.service;

import com.example.wiki.domain.Content;
import com.example.wiki.domain.Doc;
import com.example.wiki.domain.DocExample;
import com.example.wiki.mapper.ContentMapper;
import com.example.wiki.mapper.DocMapper;
import com.example.wiki.req.DocReq;
import com.example.wiki.req.DocSaveReq;
import com.example.wiki.resp.DocQueryResp;
import com.example.wiki.resp.DocResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.util.CopyUtil;
import com.example.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
public class DocService {
    private static final Logger Log = LoggerFactory.getLogger(DocService.class);
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private DocMapper docMapper;
    @Autowired
    private ContentMapper contentMapper;

    public PageResp<DocResp> list(DocReq req){

        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();

        // 分页

        PageHelper.startPage(req.getPage(), req.getSize());
        List<Doc> docList = docMapper.selectByExample(docExample);

        PageInfo<Doc> pageInfo = new PageInfo<>(docList);

        Log.info("总数目:{}",pageInfo.getTotal());
        Log.info("总页数:{}",pageInfo.getPages());

        List<DocResp> docRespList = CopyUtil.copyList(docList, DocResp.class);
        PageResp<DocResp> PageResp = new PageResp<>();
        PageResp.setTotal(pageInfo.getTotal());
        PageResp.setList(docRespList);
        return PageResp;
    }
    public void save(DocSaveReq docSaveReq){
        try{
            Doc doc = CopyUtil.copy(docSaveReq,Doc.class);
            Content content = CopyUtil.copy(docSaveReq,Content.class);
            if(ObjectUtils.isEmpty(docSaveReq.getId())){

                doc.setId(snowFlake.nextId());
                docMapper.insert(doc);
                content.setId(doc.getId());
                contentMapper.insert(content);
            } else {
                docMapper.updateByPrimaryKey(doc);
                int i = contentMapper.updateByPrimaryKeyWithBLOBs(content);
                if(i==0){
                    contentMapper.insert(content);
                }
            }

        }catch (Exception exception){
            exception.printStackTrace();

        }

    }
    public int del(Long id){
        int i = docMapper.deleteByPrimaryKey(id);
        return i;
    }
    public int del(List<String> ids){
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        criteria.andIdIn(ids);
        int i = docMapper.deleteByExample(docExample);
        return i;
    }
    /**
     * 查询所有数据
     */
    public List<DocQueryResp> all(Long ebookId){
        DocExample docExample = new DocExample();
        docExample.createCriteria().andEbookIdEqualTo(ebookId);
        docExample.setOrderByClause("sort asc");
        List<Doc> docList = docMapper.selectByExample(docExample);
        List<DocQueryResp> docRespList = CopyUtil.copyList(docList, DocQueryResp.class);
        return docRespList;
    }
    /*
    * 查询内容
    * */
    public String findContent(long id){
        Content content = contentMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(content)){
            return "";
        }else {
            return content.getContent();
        }
    }
}
